package com.stgy.lease.web.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.stgy.lease.common.exception.StgyException;
import com.stgy.lease.common.result.ResultCodeEnum;
import com.stgy.lease.model.domain.*;
import com.stgy.lease.web.api.dto.BaseSearchDto;
import com.stgy.lease.web.api.dto.RoomInfoSearchDto;
import com.stgy.lease.web.api.interceptor.StgyAuthInterceptor;
import com.stgy.lease.web.api.mapper.*;
import com.stgy.lease.web.api.service.BrowsingHistoryService;
import com.stgy.lease.web.api.service.RoomInfoService;
import com.stgy.lease.web.api.vo.*;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.List;

/**
 * @author lingx
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2024-08-28 11:00:29
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FeeKeyMapper feeKeyMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    //条件分页查询房间列表
    //首页查询房间数据 可以使用缓存
    @Override
    public Page<RoomItemVo> pageItem(RoomInfoSearchDto searchDto) {
        Page<RoomItemVo> page = searchDto.getPage(RoomItemVo.class);
        List<RoomItemVo> roomItemVos = baseMapper.selectRoomItemVoList(page, searchDto);
        page.setRecords(roomItemVos);
        return page;
    }

    //根据id查询房间详情
    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //0、room_info
        RoomInfo roomInfo = this.getById(id);
        if (roomInfo == null) {
            throw new StgyException(ResultCodeEnum.DATA_ERROR);
        }
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        //1、是否入住:租约表 如果查询到数据 表示该房间已被出租
        boolean existsFlag = leaseAgreementMapper.exists(Wrappers.lambdaQuery(LeaseAgreement.class)
                .eq(LeaseAgreement::getRoomId, id)
                .in(LeaseAgreement::getStatus, 1, 2, 5, 7)
                .last("limit 1"));
        roomDetailVo.setIsCheckIn(existsFlag);

        //2、公寓表
        //2.1查询公寓的数据
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        //2.2查询公寓的图片
        List<GraphInfo> apartmentGraphInfos = graphInfoMapper.selectList(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId, roomInfo.getApartmentId())
                .eq(GraphInfo::getItemType, 1));
        apartmentItemVo.setGraphVoList(apartmentGraphInfos);
        //2.3查询公寓的标签列表
        List<LabelInfo> apartmentLabelInfos =
                labelInfoMapper.selectByApartmentId(roomInfo.getApartmentId());
        apartmentItemVo.setLabelInfoList(apartmentLabelInfos);
        //2.4查询公寓中所有房间的最低租金
        BigDecimal minRent = baseMapper.selectMinRentByApartmentId(roomInfo.getApartmentId());
        apartmentItemVo.setMinRent(minRent);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);

        //3、房间的配套列表
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfos);

        //4、房间的标签列表
        List<LabelInfo> labelInfos = labelInfoMapper.selectByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfos);

        //5、房间的图片列表
        List<GraphInfo> graphInfoList = graphInfoMapper.selectByRoomId(id);
        roomDetailVo.setGraphVoList(graphInfoList);

        //6、房间的租期列表
        List<LeaseTerm> leaseTerms = leaseTermMapper.selectByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTerms);

        //7、支付方式列表
        List<PaymentType> paymentTypes = paymentTypeMapper.selectByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypes);

        //8、房间的属性值列表
        List<AttrValueVo> attrValueMappers = attrValueMapper.selectAttrValueVosByRooms(id);
        roomDetailVo.setAttrValueVoList(attrValueMappers);

        //9、房间所属公寓的杂费列表
        List<FeeValueVo> feeKeyMappers = feeKeyMapper.selectFeeKeyVosByApartmentId(roomInfo.getApartmentId());
        roomDetailVo.setFeeValueVoList(feeKeyMappers);

        //异步保存浏览记录
        browsingHistoryService.saveOrUpdateAsync(StgyAuthInterceptor.THREAD_LOCAL.get().getId(),
                id);

        return roomDetailVo;
    }

    //根据公寓id分页查询房间列表
    @Override
    public Page<RoomItemVo> pageItemByApartmentId(BaseSearchDto baseSearchDto) {
        Page<RoomItemVo> page = baseSearchDto.getPage(RoomItemVo.class);
        List<RoomItemVo> roomItemVos = baseMapper.selectApartmentItemVoList(page, baseSearchDto);
        page.setRecords(roomItemVos);
        return page;
    }
}